/**
 *
 */
package org.jim.core.packets;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.google.common.collect.Lists;
import com.im.app.utils.uuid.IdUtils;
import org.apache.commons.collections4.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * 版本: [1.0]
 * 功能说明: 用户群组
 * 作者: WChao 创建时间: 2017年9月21日 下午1:54:04
 */
@TableName(value = "Groups")
public class Group extends Message{

	private static final long serialVersionUID = -3817755433171220952L;
	/**
	 * 群组ID
	 */
	@TableId(type = IdType.ASSIGN_UUID)
	private String groupId;
	/**
	 * 群组名称
	 */
	private String name;
	/**
	 * 群组头像
	 */
	private String avatar;
	/**
	 * 在线人数
	 */
	@TableField(exist = false)
	private int online;
	/**
	 * 组用户
	 */
	@TableField(exist = false)
	private List<GroupMember> members;
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
	private Date createTimers;

	/**
	 * 创建者ID
	 */
	private String ownerId;
	/**
	 * 是否全休禁言 0 不禁言 1 禁言
	 */
	private int mute;
	/**
	 * 公告
	 */
	private String notice;
	/**
	 * 简介
	 */
	private String introduce;
	/**
	 * 群状态 0正常 1解散 2封禁
	 */
	private int status;
	/**
	 * 欢迎语
	 */
	private String welcome;
	/**
	 * 卡密
	 */
	private String kamicode;

	private Group(){}

	private Group(String groupId , String name, String avatar, Integer online, List<GroupMember> members, JSONObject extras){
		this.groupId = groupId;
		this.name = name;
		this.avatar = avatar;
		this.online = online == null ? 0 : online;
		this.members = members;
		this.extras = extras;
	}

	public static Builder newBuilder(){
		return new Builder();
	}

	public String getOwnerId() {
		return ownerId;
	}

	public void setOwnerId(String ownerId) {
		this.ownerId = ownerId;
	}

	public int getMute() {
		return mute;
	}

	public void setMute(int mute) {
		this.mute = mute;
	}

	public Date getCreateTimers() {
		return createTimers;
	}

	public void setCreateTimers(Date createTimers) {
		this.createTimers = createTimers;
	}

	public String getWelcome() {
		return welcome;
	}

	public void setWelcome(String welcome) {
		this.welcome = welcome;
	}

	public String getNotice() {
		return notice;
	}

	public void setNotice(String notice) {
		this.notice = notice;
	}

	public String getIntroduce() {
		return introduce;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public void setIntroduce(String introduce) {
		this.introduce = introduce;
	}

	public String getGroupId() {
		return groupId;
	}

	public void setGroupId(String groupId) {
		this.groupId = groupId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAvatar() {
		return avatar;
	}

	public void setAvatar(String avatar) {
		this.avatar = avatar;
	}

	public int getOnline() {
		return online;
	}

	public void setOnline(int online) {
		this.online = online;
	}

	public String getKamicode() {
		return kamicode;
	}

	public void setKamicode(String kamicode) {
		this.kamicode = kamicode;
	}


	public List<GroupMember> getMembers() {
		return members;
	}

	public void setMembers(List<GroupMember> members) {
		this.members = members;
	}

	public static class Builder extends Message.Builder<Group, Builder>{
		/**
		 * 群组ID
		 */
		private String groupId;
		/**
		 * 群组名称
		 */
		private String name;
		/**
		 * 群组头像
		 */
		private String avatar;
		/**
		 * 在线人数
		 */
		private Integer online;
		/**
		 * 组用户
		 */
		private List<GroupMember> members = null;

		public Builder(){};

		public Builder groupId(String groupId) {
			this.groupId = groupId;
			return this;
		}
		public Builder name(String name) {
			this.name = name;
			return this;
		}
		public Builder avatar(String avatar) {
			this.avatar = avatar;
			return this;
		}
		public Builder setChatType(Integer online) {
			this.online = online;
			return this;
		}
		public Builder addUser(GroupMember member) {
			if(CollectionUtils.isEmpty(members)){
				members = Lists.newArrayList();
			}
			members.add(member);
			return this;
		}
		@Override
		protected Builder getThis() {
			return this;
		}

		@Override
		public Group build(){
			return new Group(this.groupId , this.name , this.avatar , this.online , this.members, this.extras);
		}
	}

	public Group clone(){
		Group group = Group.newBuilder().build();
		BeanUtil.copyProperties(this, group,"users");
		return group;
	}

	public ChatBody updateMessageTip(String message){

		return chatBody(message);
	}

	public ChatBody muteMessageTip(){
		String message = mute == 0 ? "取消全体成员禁言" : "全体成员已禁言";
		return chatBody(message);
	}


	public ChatBody createGroupMessageTip(String userName){
		StringBuilder message = new StringBuilder(userName+"创建了群聊（");
		for (int i = 0;i<getMembers().size();i++){
			GroupMember groupMember = getMembers().get(i);
			message.append(groupMember.getNickname());
			if (i < getMembers().size() - 1){
				message.append(",");
			}else {
				message.append("）");
			}
		}

		return chatBody(message.toString());
	}

	private ChatBody chatBody(String message){
		ChatBody chatBody = new ChatBody();
		chatBody.setCmd(Command.COMMAND_CHAT_REQ.getNumber());
		chatBody.setCreateTime(new Date().getTime());
		chatBody.setId(IdUtils.simpleUUID());
		chatBody.setContent(message);
		chatBody.setGroupId(getGroupId());
		chatBody.setFromType(0);
		chatBody.setAvatar(getAvatar());
		chatBody.setNickName(getName());
		chatBody.setFrom(getOwnerId());
		chatBody.setMsgType(0);
		chatBody.setChatType(ChatType.CHAT_TYPE_PUBLIC.getNumber());
		return chatBody;
	}

}
